package com.gaohe.controller;

import com.alibaba.fastjson.JSON;
import com.gaohe.Listener.countLis;
import com.gaohe.pojo.Manager;
import com.gaohe.pojo.User;
import com.gaohe.service.ManagerService;
import com.gaohe.service.UserService;
import com.gaohe.untils.CheckCodeUtil;
import com.gaohe.vo.LoginVo;
import com.gaohe.vo.LoginVo_Mn;
import com.gaohe.vo.RegisterVo;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

@WebServlet("/manager/*")
public class ManagerServlet extends BaseServlet {
    public void login(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        LoginVo_Mn loginVo_mn = JSON.parseObject(s, LoginVo_Mn.class);
        System.out.println(loginVo_mn.getUsername());
        ManagerService managerService = new ManagerService();
        Manager manager = managerService.findByUsername(loginVo_mn.getUsername());
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (manager == null){
            writer.write("nouser");
            return;//跳出if语句，不再执行下面语句
        } else {//用户存在
            if (manager.getPassword().equals(loginVo_mn.getPassword())){//密码正确
                session.setAttribute("manager",manager);//将根据字段查到的user传给服务器的session中
                if (loginVo_mn.getState() == true){//记住密码
                    addCookie(resp, manager,60*60*24*7);//以秒为单位
                }else {
                    addCookie(resp, manager,0);
                }
                writer.write("succeed");
                return;
            }else {
                writer.write("failed");
                return;
            }
        }
    }
    private void addCookie(HttpServletResponse resp, Manager manager, int i) {
//        Cookie username = new Cookie("username", user.getUsername());
        Cookie username = new Cookie("username", URLEncoder.encode(manager.getUsername()));
        Cookie password = new Cookie("password", manager.getPassword());
        username.setMaxAge(i);
        password.setMaxAge(i);
        resp.addCookie(username);
        resp.addCookie(password);
    }
    public void getName(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        Manager manager = (Manager) session.getAttribute("manager");
        String username = manager.getUsername();
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write(username);
    }
    public void logout(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.removeAttribute("manager");
        resp.getWriter().write("1");//写任意内容均可，因为前端函数中不需要resp传的data
    }
    public void count(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String count = countLis.count+"";
//        response.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write(count);
    }
}
